﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using ECS.DAL;
using ECS.DAL.Component;
using ECS.BLL.Entity;
using ECS.Framework;
namespace ECS.BLL.Component
{
    public class ResultCheckMAWB
    {
        public CHECK_MAWB_TYPE Status { get; set; }
        public DataSet Data { get; set; }
        public DataSet DataHAWB { get; set; }
        public CHECK_FBL fblStatus { get; set; }
    }
    public enum CHECK_FBL
    {
        Gen,
        NotGen
    }
    public enum CHECK_MAWB_TYPE
    {
        Add,
        Search,
        None,
        Fail,
        Pass,
        Warning
    }
    class AddMAWB
    {
        #region private properties
        #endregion
        #region private methods
        private string checkFBLParameter(string carrier,string fblFlightNo,string currentFblFlightNo)
        {
            string fblStatus = string.Empty;
            string result = string.Empty;
            SearchInfo search = new SearchInfo();
            fblStatus = search.CheckFBL(carrier, fblFlightNo, currentFblFlightNo);
            if (fblStatus == "YES")
            {
                result = "YES";
            }
            else
            {
                result = "NO";
            }                     
            return result;
        }
        private ResultCheckMAWB getConsol(string status, string hawb, DataSet ds, string datasource,string carrier,string fblFlightNo,string currentFblFlightNo)
        {
            ResultCheckMAWB result = new ResultCheckMAWB();
            DataSet dsHawb = null;
            string fbl = string.Empty;
            if (status == "PASS" && datasource == "LOCAL")
            {
                dsHawb = CheckHAWB(hawb, ds);
                if (dsHawb != null)
                {
                    result.Status = CHECK_MAWB_TYPE.Search;
                    result.Data = ds;
                }
                else
                {
                    result.Status = CHECK_MAWB_TYPE.Add;
                }
            }
            else if (status == "PASS" && datasource == "CARGOSPOT")
            {
                if (carrier == "CX")
                {
                    result.Status = CHECK_MAWB_TYPE.Add;
                    fbl = checkFBLParameter(carrier, fblFlightNo, currentFblFlightNo).ToString();
                    if (fbl == "YES")
                    {
                        result.fblStatus = CHECK_FBL.Gen;
                    }
                    else
                    {
                        result.fblStatus = CHECK_FBL.NotGen;
                    }
                }
                else
                {
                    result.Status = CHECK_MAWB_TYPE.Add;
                }
            }
            else
            {
                result.Status = CHECK_MAWB_TYPE.Add;
            }
            return result;
        }
        private ResultCheckMAWB getNonConsol(string status, DataSet ds, string datasource, string carrier, string fblFlightNo, string currentFblFlightNo)
        {
            ResultCheckMAWB result = new ResultCheckMAWB();
            string fbl = string.Empty;
            if (status == "PASS" && datasource == "LOCAL")
            {
                    result.Status = CHECK_MAWB_TYPE.Search;
                    result.Data = ds;
            }
            else if (status == "PASS" && datasource == "CARGOSPOT")
            {
                if (carrier == "CX")
                {
                    result.Status = CHECK_MAWB_TYPE.Add;
                    fbl = checkFBLParameter(carrier, fblFlightNo, currentFblFlightNo).ToString();
                    if (fbl == "YES")
                    {
                        result.fblStatus = CHECK_FBL.Gen;
                    }
                    else
                    {
                        result.fblStatus = CHECK_FBL.NotGen;
                    }
                }
                else
                {
                    result.Status = CHECK_MAWB_TYPE.Add;
                }
            }
            else
            {
                result.Status = CHECK_MAWB_TYPE.Add;
            }
            return result;
        }
        #endregion
        #region public methods
        public ResultCheckMAWB CheckMAWB(string MAWB, string prefix, string HAWB, string serial, string shipmenttype, string UserID, string Device, string AppID)
        {
            //string stype = string.Empty;
            string status = string.Empty;
            //string datasource = string.Empty;
            string hawb = string.Empty;
            string carrier = string.Empty;
            string fblFlightNo = string.Empty;
            string currentFblFlightNo = string.Empty;
            ResultCheckMAWB result = new ResultCheckMAWB();
            DataSet ds = new DataSet();
            using (Entities entity = new Entities())
            {
                StringBuilder sqlString = new StringBuilder();
                sqlString.Append("usp_GetMAWB");
                entity.Open();
                DbCommand cmd = entity.CreateCommand(sqlString.ToString());
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Add(entity.CreateParameter("@in_vchApplicationID", AppID));
                cmd.Parameters.Add(entity.CreateParameter("@in_vchDevice", Device));
                cmd.Parameters.Add(entity.CreateParameter("@in_vchUserID", UserID));
                cmd.Parameters.Add(entity.CreateParameter("@in_vchMAWB", MAWB));
                cmd.Parameters.Add(entity.CreateParameter("@in_vchMAWBPrefix", prefix));
                cmd.Parameters.Add(entity.CreateParameter("@in_vchMAWBSerial", serial));
                if (shipmenttype == "")
                {
                    cmd.Parameters.Add(entity.CreateParameter("@in_vchHAWB", HAWB));
                }
                DbDataAdapter adapter = entity.CreateDataAdapter(cmd);
                adapter.Fill(ds);
                status = ds.Tables[0].Rows[0]["Status"].ToString();
                if (status == "PASS") //reult from store procedure
                {
                    E_ICS.GlobalVariable.chkShipmentType = ds.Tables[0].Rows[0]["shipment_type"].ToString();
                    E_ICS.GlobalVariable.chkDatasource = ds.Tables[0].Rows[0]["source"].ToString();
                    carrier = ds.Tables[0].Rows[0]["carrier"].ToString();
                    fblFlightNo = ds.Tables[0].Rows[0]["flight_number"].ToString();
                    currentFblFlightNo = E_ICS.GlobalVariable.fbl_fltNO.Substring(2, E_ICS.GlobalVariable.fbl_fltNO.Length - 2);
                    if (shipmenttype == "CONSOL") // radio button
                    {
                        if (E_ICS.GlobalVariable.chkShipmentType == "CONSOL") // getShipment type
                        {
                            result = getConsol(status, HAWB, ds, E_ICS.GlobalVariable.chkDatasource,carrier,fblFlightNo,currentFblFlightNo);
                        }
                        else if (E_ICS.GlobalVariable.chkShipmentType == "")
                        {
                            result = getConsol(status, HAWB, ds, E_ICS.GlobalVariable.chkDatasource, carrier, fblFlightNo, currentFblFlightNo);
                        }
                        else if (E_ICS.GlobalVariable.chkDatasource == "CARGOSPOT")
                        {
                            //if (CommonMessageBox.ShowMessageConfirmYN("This MAWB is already in CARGOSPOT. Are you want to add?") == DialogResult.Yes)
                            //{
                            //    result = getConsol(status, HAWB, ds, E_ICS.GlobalVariable.chkDatasource);
                            //}
                            //else
                            //{
                            //    result.Status = CHECK_MAWB_TYPE.None;
                            //}
                            result = getConsol(status, HAWB, ds, E_ICS.GlobalVariable.chkDatasource, carrier, fblFlightNo, currentFblFlightNo);
                        }
                        else
                        {
                            result.Status = CHECK_MAWB_TYPE.None;
                            CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_Main2_Msgbox_Check_Shipment_type"));                  
                        }
                    }
                    else // radio button
                    {
                        if (E_ICS.GlobalVariable.chkShipmentType == "NONCON" && E_ICS.GlobalVariable.chkDatasource == "LOCAL") 
                        {
                            if (!string.IsNullOrEmpty(prefix) && !string.IsNullOrEmpty(serial))
                            {
                                result = getNonConsol(status, ds, E_ICS.GlobalVariable.chkDatasource,carrier,fblFlightNo,currentFblFlightNo);
                            }
                            else
                            {
                                CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_Main2_Msgbox_Check_MAWB"));
                            }
                        }
                        else if (E_ICS.GlobalVariable.chkShipmentType == "")
                        {
                            if (!string.IsNullOrEmpty(prefix) && !string.IsNullOrEmpty(serial))
                            {
                                result = getNonConsol(status, ds, E_ICS.GlobalVariable.chkDatasource, carrier, fblFlightNo, currentFblFlightNo);
                            }
                            else
                            {
                                CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_Main2_Msgbox_Check_MAWB"));
                            }
                        }
                        else if (E_ICS.GlobalVariable.chkShipmentType == "NONCON" && E_ICS.GlobalVariable.chkDatasource == "CARGOSPOT")
                        {
                            //if (CommonMessageBox.ShowMessageConfirmYN("This MAWB is already in CARGOSPOT. Are you want to add?") == DialogResult.Yes)
                            //{
                            //    result = getNonConsol(status, ds, E_ICS.GlobalVariable.chkDatasource);
                            //}
                            //else
                            //{
                            //    result.Status = CHECK_MAWB_TYPE.None;
                            //}
                            result = getNonConsol(status, ds, E_ICS.GlobalVariable.chkDatasource, carrier, fblFlightNo, currentFblFlightNo);
                        }
                        else
                        {
                            result.Status = CHECK_MAWB_TYPE.None;
                            CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_Main2_Msgbox_Check_Shipment_type"));
                        }
                    }
                }
                else
                {
                   
                    E_ICS.GlobalVariable.chkShipmentType = "";
                }
            }
            return result; 
        }
        public DataSet CheckHAWB(string hawb, DataSet ds)
        {
            //bool result = false;
            DataSet data = null;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (hawb == ds.Tables[0].Rows[i]["hawb"].ToString())
                {
                    //result = true;
                    data = new DataSet();
                    //var desRow = ds.Tables[0].NewRow();
                    //var srcRow = ds.Tables[0].Rows[i];
                    //desRow.ItemArray = srcRow.ItemArray.Clone() as object[];
                    DataTable dt = new DataTable("test");
                        dt = ds.Tables[0].Clone();
                        DataRow dr = ds.Tables[0].Rows[i];
                        dt.ImportRow(dr);
                    data.Tables.Add(dt);
                    break;
                }
            }
            return data;
        }
        #endregion
    }
}
